#include "controlwidget.h"
#include "ui_controlwidget.h"
#include "srmediaplayer.h"

ControlWidget::ControlWidget(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::ControlWidget)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);  // 设置无边框
    setAttribute(Qt::WA_TranslucentBackground); // 设置背景透明
    installEventFilter(this); // 安装事件过滤器

    // 获取主窗口指针
    mainWindow = qobject_cast<SRMediaPlayer *>(parent); // 使用成员变量

    // 连接按钮信号到主窗口的槽函数
    connect(ui->PreviousButton, &QPushButton::clicked, mainWindow, &SRMediaPlayer::on_PreviousButton_clicked);
    connect(ui->NextButton, &QPushButton::clicked, mainWindow, &SRMediaPlayer::on_NextButton_clicked);
    connect(ui->PlayButton, &QPushButton::clicked, mainWindow, &SRMediaPlayer::on_PlayButton_clicked);
    connect(ui->PauseButton, &QPushButton::clicked, mainWindow, &SRMediaPlayer::on_PauseButton_clicked);

    // 连接进度条信号到主窗口的槽函数
    connect(ui->ProgressSlider, &QSlider::sliderMoved, mainWindow, &SRMediaPlayer::on_sliderMoved);
    connect(mainWindow, &SRMediaPlayer::progressSliderValueChanged, this, &ControlWidget::on_progressSliderValueChanged);
    connect(mainWindow, &SRMediaPlayer::durationChanged, this, &ControlWidget::on_durationChanged);

    // 音量滑块
    ui->SoundSlider->setRange(0, 100);
    ui->SoundSlider->setValue(50);  // 设置初始音量为 50

}

ControlWidget::~ControlWidget()
{
    delete ui;
}

// 响应播放位置变化的信号
void ControlWidget::on_progressSliderValueChanged(int position)
{
    ui->ProgressSlider->setSliderPosition(position);
}

// 响应媒体总时长变化的信号
void ControlWidget::on_durationChanged(qint64 duration)
{
    ui->ProgressSlider->setRange(0, duration);
}

// 音量设置
void ControlWidget::on_SoundSlider_valueChanged(int value)
{
    emit volumeChanged(value);  // 发出信号，携带新的音量值
}
